#include "EmailSuggest.h"

EmailSuggest::EmailSuggest(QLineEdit *parent): QObject(parent)
{
    popup = new QTreeWidget;
    popup->setColumnCount(1);
    popup->setWindowFlags(Qt::Popup);
    popup->setFocusPolicy(Qt::NoFocus);
    popup->setMouseTracking(true);
    popup->setUniformRowHeights(true);
    popup->setRootIsDecorated(false);
    popup->setFocusProxy(parent);
    popup->setEditTriggers(QTreeWidget::NoEditTriggers);
    popup->setSelectionBehavior(QTreeWidget::SelectRows);
    popup->setFrameStyle(QFrame::Box | QFrame::Plain);
    popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    popup->header()->hide();

    popup->installEventFilter(this);

    connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(DoneSuggest()));

}

EmailSuggest::~EmailSuggest()
{
    delete popup;
}

void EmailSuggest::DoneSuggest()
{
    popup->hide();
    edit->setFocus();
    QTreeWidgetItem *item = popup->currentItem();
    if(item)
    {
        edit->setText(item->text(0));
    }
}

void EmailSuggest::EmailContact(QStringList accountsList)
{
    for(int a=0;a<accountsList.size();a++)
    {
        QStringList record = db.EmailContact(accountsList.at(a));
        emailList +=record;
    }

}

void EmailSuggest::ShowSuggest(QLineEdit *editor, QString str)
{
    edit = editor;

    popup->setUpdatesEnabled(false);
    popup->clear();

    if(str.isEmpty())
    {
        suggest.clear();
        return;
    }
    str.append(".*");
    str.prepend(".*");
    suggest = emailList.filter(QRegExp(str));

    if(suggest.isEmpty())
        return;

    for (int i = 0; i < suggest.size(); ++i)
    {
        QTreeWidgetItem * item;
        item = new QTreeWidgetItem(popup);
        item->setText(0,suggest.at(i));
    }

    popup->setCurrentItem(popup->topLevelItem(0));
    popup->resizeColumnToContents(0);
    popup->adjustSize();
    popup->setUpdatesEnabled(true);

    int h = popup->sizeHintForRow(0) * qMin(7, suggest.count()) + 3;
    popup->resize(popup->width(), h);

    popup->move(edit->mapToGlobal(QPoint(0, edit->height())));
    popup->setFocus();
    popup->show();
}

bool EmailSuggest::eventFilter(QObject *obj, QEvent *ev)
{
    if (obj != popup)
        return false;

    if (ev->type() == QEvent::MouseButtonPress) {
        popup->hide();
        edit->setFocus();
        return true;
    }

    if (ev->type() == QEvent::KeyPress) {

        bool consumed = false;
        int key = static_cast<QKeyEvent*>(ev)->key();
        switch (key) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
            DoneSuggest();
            consumed = true;

        case Qt::Key_Escape:
            edit->setFocus();
            popup->hide();
            consumed = true;

        case Qt::Key_Up:
        case Qt::Key_Down:
        case Qt::Key_Home:
        case Qt::Key_End:
        case Qt::Key_PageUp:
        case Qt::Key_PageDown:
            break;

        default:
            edit->setFocus();
            edit->event(ev);
            if(suggest.isEmpty())
                popup->hide();
            break;
        }

        return consumed;
    }

    return false;
}
